#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <sched.h>
#include <signal.h>
#include <unistd.h>

#define STACK_SIZE (1024 * 1024)
static char container_stack[STACK_SIZE];

// 传给execv的命令
char *const container_args[] = {
    "/bin/bash",
    NULL};

int container_main(void *arg)
{
    printf("Container - 进入容器!\n");
    /* 设置hostname,第二个参数是hostname的长度。 所以我这里是8*/
    sethostname("yunsheng", 8); 
    execv(container_args[0], container_args);
    printf("Something's wrong!\n");
    return 1;
}

int main()
{
    printf("Parent父进程 - start a container!\n");
    int container_pid = clone(container_main, container_stack + STACK_SIZE,
                              CLONE_NEWUTS | SIGCHLD, NULL); /*启用CLONE_NEWUTS Namespace隔离 */
    waitpid(container_pid, NULL, 0);
    printf("Parent - container stopped!\n");
    return 0;
}